package me.jzn.alib.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

import me.jzn.core.Core;
import me.jzn.core.exceptions.ShouldNotRunHereException;

/**
 * @author xqs @date 2019/12/4
 */
public class NotifUtil {
    /**
     * 必要属性有三项
     * 小图标，通过 setSmallIcon() 方法设置
     * 标题，通过 setContentTitle() 方法设置
     * 内容，通过 setContentText() 方法设置
     * <p>
     */
    public static NotificationCompat.Builder createBuilder(Context ctx, NotifConf conf) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, conf.channelId);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = ContextCompat.getSystemService(ctx, NotificationManager.class);
            NotificationChannel channel = manager.getNotificationChannel(conf.channelId);
            if (channel == null) {
                channel = new NotificationChannel(conf.channelId, conf.channelName, NotificationManager.IMPORTANCE_DEFAULT);
                if (conf.enableLights != null) {
                    channel.enableLights(conf.enableLights);
                    if (conf.lightColor != null) {
                        channel.setLightColor(conf.lightColor);
                    }
                }
                if (conf.channelDescription != null) {
                    channel.setDescription(conf.channelDescription);
                }

                if (conf.showBadge != null) {
                    channel.setShowBadge(conf.showBadge);
                }
                if (conf.sound != null) {
                    channel.setSound(conf.sound, conf.soundAttr);
                    builder.setSound(conf.sound);
                }
                if (conf.vibration != null) {
                    channel.setVibrationPattern(conf.vibration);
                }
                channel.setImportance(conf.importance);
                if (conf.allowBubbles != null && Build.VERSION.SDK_INT >= 29) {
                    channel.setAllowBubbles(conf.allowBubbles);
                }
                //            channel.enableLights(true); //是否在桌面icon右上角展示小红点
                //            channel.setLightColor(Color.GREEN); //小红点颜色
                //            channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
                manager.createNotificationChannel(channel);
            }
        }
        return builder;
    }

    public static void notify(Context ctx, int id, Notification notification) {
        if (Core.isDebug()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationManager manager = ContextCompat.getSystemService(ctx, NotificationManager.class);
                NotificationChannel channel = manager.getNotificationChannel(notification.getChannelId());
                if (channel == null) {
                    throw new ShouldNotRunHereException("no channel exist.NotifUtil.init should call first!");
                }
            }
        }
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(ctx);
        managerCompat.notify(id, notification);
    }

    public static void cancel(Context ctx, int notifId) {
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(ctx);
        managerCompat.cancel(notifId);
    }

    public static class NotifConf {
        @NonNull
        private String channelId;
        @Nullable
        private String channelName;

        private String channelDescription;

        private Boolean enableLights;
        private Integer lightColor;
        // 气泡
        private Boolean allowBubbles;
        // 角标
        private Boolean showBadge;

        private int importance = NotificationManager.IMPORTANCE_DEFAULT;

        private Uri sound;
        private AudioAttributes soundAttr;
        private long[] vibration;

        public NotifConf(@NonNull String channelId, @Nullable String channelName) {
            this.channelId = channelId;
            this.channelName = channelName;
        }

        public String isChannelDescription() {
            return channelDescription;
        }

        public void setChannelDescription(String channelDescription) {
            this.channelDescription = channelDescription;
        }

        public void setEnableLight(boolean enableLight) {
            this.enableLights = enableLight;
        }

        public void setLightColor(@ColorInt int lightColor) {
            this.lightColor = lightColor;
        }

        public void setAllowBubbles(boolean allowBubbles) {
            this.allowBubbles = allowBubbles;
        }


        public void setShowBadge(boolean showBadge) {
            this.showBadge = showBadge;
        }

        public void setImportance(int importance) {
            this.importance = importance;
        }


        public void setSound(Uri sound, AudioAttributes soundAttr) {
            this.sound = sound;
            this.soundAttr = soundAttr;
        }

        public void setVibration(long[] vibration) {
            this.vibration = vibration;
        }
    }
}

